package operation;

import JDBC.JDBCUtils;
import Medicine.Pharmacy;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.Scanner;


public class ChangePriceOfMed implements IOPeration{
    @Override
    public void work(Pharmacy pharmacy) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要更新价格的药品名称: ");
        String name = sc.nextLine();
        System.out.println("请输入新的药品价格: ");
        double newPrice = sc.nextDouble();
        updatePrice(name, newPrice);
    }
    public static void updatePrice(String name, double newPrice) throws SQLException {
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "UPDATE Medicine SET Price=? WHERE Name=?";
        Object[] params = {newPrice, name};
        int count = qr.update(sql, params);
        if (count < 1) {
            System.out.println("更新价格失败，没有找到对应的药品或更新不成功。");
        } else {
            System.out.println("更新价格成功！");
        }
    }

}



